package boot

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/kataras/iris/v12"
	"github.com/spf13/viper"
	"os"
	"wit-admin-go/conf"
	"wit-admin-go/system/zaplogger"
)

// InitConfig 加载配置文件
func init() {
	var config string
	if configEnv := os.Getenv(conf.ConfigEnv); configEnv == "" {
		config = conf.ConfigFile
		log.Log.Info("您正在使用config的默认值,config的路径为", conf.ConfigFile)
	} else {
		config = configEnv
		log.Log.Info("您正在使用CONFIG环境变量,config的路径为", config)
	}

	v := viper.New()
	v.SetConfigFile(config)
	err := v.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("fatal error config file: %s ", err))
	}
	v.WatchConfig()

	v.OnConfigChange(func(e fsnotify.Event) {
		log.Log.Info("配置文件已更改:", e.Name)
		if err := v.Unmarshal(&conf.CONFIG); err != nil {
			panic(err)
		}
	})

	if err := v.Unmarshal(&conf.CONFIG); err != nil {
		panic(err)
	}
}

// WitLog 日志配置
func WitLog(app *iris.Application) {
	//business.Logger().SetLevel("debug")
	//log := logger.New(logger.Config{
	//	Status:             true,
	//	IP:                 true,
	//	Method:             true,
	//	Path:               true,
	//	Query:              true,
	//	MessageContextKeys: []string{"logger_message"},
	//	MessageHeaderKeys:  []string{"User-Agent"},
	//})
	////business.Use(recover.New())
	//business.Use(log)
	log.InitLogger()
	defer log.Logger.Sync()
}
